from django.shortcuts import render
from django.views.decorators.http import require_http_methods
from django.http import HttpResponse
import json
import csv
# Create your views here.


def hello(request):
    #return HttpResponse("hello world")
    # print(request.body)
    # print(json.loads(request.body('name')))
    # print(request.GET.get('name'))
    # print(request.POST.get('pwd'))
    return render(request, 'login.html')


def is_login(request):
    def is_login(request):
        username = request.POST.get('username')
        pwd = request.POST.get('pwd')

        # if username and pwd:
        #     with open(r'user.csv','r')as f:
        #         readers =csv.reader(f)
        #         for i in readers:
        #             if i[0] ==username and i[1] == pwd:
        #                 return render(request, 'home.html', {'user': username})

        if username == 'admin' and pwd == '123456':
                return render(request, 'home.html', {'user': username})
            else:
                return render(request, 'error.html', {'msg': '用户名或密码错误'})
        else:
            return render(request, 'error.html', {'msg': '缺少必要参数'})



@require_http_methods(["GET"])
def bugs(request):
    data = {
        "business_autoFans_J":[{"8":14},  {"9":15}, {"10":9}],
        "autoAX":[{"8":7},  {"9":32}, {"10":0}],
        "autoAX_admin":[{"8":5},  {"9":13}, {"10":2}],
    }
    month = request.GET.get('month')
    sum = 0
    if month:
        if month.isdigit() and int(month) in range(1, 13):
            for info in data.values():
                for v in info:
                    if int(list(v.keys())[0]) == int(month):
                        sum = sum + list(v.values())[0]
            return HttpResponse('%d月 共有 %d 个bug' % (int(month), sum))
        else:
            return HttpResponse('month格式错误')
    else:
        return HttpResponse('月份参数month为必填项')